/**
 * 机构管理主界面
 * @author Linzongxue 2011-12-11
 */
define(function(){
	var DepartMainPanel = Ext.extend(Ext.ux.tree.TreeGrid, {
		initComponent: function(){
			var mainPanel = this;
			Ext.apply(this, {
				border: false,
				stripeRows: true,
			    enableHdMenu: false,
			    enableSort: false,
				useArrows: true,
		        animate: true,
			    loadMask: true,
		        //enableDD: true,
		        tbar: au.permit([
	            	{text: '添加', permitId:'sys.depart.add', handler: this.addDepart, iconCls: 'icon-add', scope: this, tooltip:'添加机构'},
	            	{text: '修改', permitId:'sys.depart.modify', handler: this.modifyDepart, iconCls: 'icon-edit',scope: this, tooltip:'修改机构'},
	            	{text: '删除', permitId:'sys.depart.delete', handler: this.removeDepart, iconCls: 'icon-delete',scope: this, tooltip:'删除机构'},
					'-',
					{text: '刷新', handler: this.refresh, iconCls: 'icon-refresh', scope: this}
	            ]),
				root: new Ext.tree.AsyncTreeNode({
					id:'root',
					listeners:{
			        	expand: function(node){ //默认展开到第二级
			        		node.eachChild(function(n){n.expand();});
		        		}
		        	}
				}),
		        rootVisible: false,
				dataUrl: 'sys/depart/getDepartTreeGridData.do',
		        columns:[
		            {header: '机构名称', dataIndex: 'name', width: 500},
		            {header: '排序号', dataIndex: 'sortNum', width: 50}
		        ],
		        listeners:{
		        	dblclick: function(node, e){
		        		if (this.getSelectionModel().getSelectedNode()) this.modifyDepart();
		        	}
		        }
			});
			DepartMainPanel.superclass.initComponent.call(this);
		},
		addDepart: function(){
			var parentNode = this.getSelectionModel().getSelectedNode();
			use('sys.depart.DepartWindow', function(Win){
				new Win({
					title: '添加机构',
					parentDepart: parentNode || this.getRootNode(),
					callback: this.saveCallback,
					cbScope: this
				}).show();
			}, this);
		},
		modifyDepart: function(){
			var node = this.getSelectionModel().getSelectedNode();
			if (!node){
				cu.hint('请选择要修改的机构');
				return;
			}
			use('sys.depart.DepartWindow', function(Win){
				new Win({
					title: '修改机构',
					modifiedId: node.attributes.id,
					parentDepart: node.parentNode,
					callback: function(parentNodePath, depart){
						if (node.parentNode.getPath() != parentNodePath){ //修改了父节点，原来的节点要删除
							node.remove();
						}
						//刷新父节点，并定位被修改的节点
						this.saveCallback(parentNodePath, depart);
					},
					cbScope: this
				}).show();
			}, this);
		},
		removeDepart: function(){
			var nodes = this.getChecked();
			if (!nodes || nodes.length == 0){
				cu.hint('请选择要删除的机构');
				return;
			}
			cu.confirm('确定要删除选中的机构吗？', function(){
				cu.post({
					url: 'sys/depart/deleteDeparts.do', 
					params: {ids: lu.pluck(nodes, 'attributes.id')},
					success: function(){
						Ext.each(nodes, function(n){
							n.ownerTree && n.remove();
						});
					}, 
					scope: this
				});
			}, this);
		},
		//添加和修改机构后的回调方法，刷新并定位保存后的机构
		saveCallback: function(parentNodePath, depart){
			this.selectPath(parentNodePath, 'id', function(success, node){
				var tree = node.getOwnerTree();
				tree.getLoader().load(node, function(){
					tree.selectPath(parentNodePath + '/' + depart.id);
				});
			});
		},
		refresh: function(){
			this.getLoader().load(this.root, function(n){n.fireEvent('expand', n);});
		}
	});
	return DepartMainPanel;
});